package com.cxyd.modules.wcs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.modules.log.service.SysLogWorkService;
import com.cxyd.modules.wcs.dao.PlcItemDao;
import com.cxyd.modules.wcs.entity.PlcItemEntity;
import com.cxyd.modules.wcs.service.PlcItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class PlcItemServiceImpl implements PlcItemService {
    @Resource
    private PlcItemDao itemDao;
    @Resource
    private SysLogWorkService logWorkService;

    @Override
    public boolean signalJudge(String contNo, String applyType, String stnNo) {
//        applyType写死为'GET'与'PUT'
        if (StrUtil.isBlankIfStr(applyType) || StrUtil.isBlankIfStr(stnNo)) {
            return false;
        }
        String pCode = "0";
        String pMsg = "CTU在站点[" + stnNo + "]进行[" + applyType + "]请求成功!料箱[" + contNo + "]";
        String workName = "CTU取放申请判断";
        String workProc = "PlcItemService.signalJudge";

        QueryWrapper<PlcItemEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PlcItemEntity::getStnNo, stnNo)
                .eq(PlcItemEntity::getItemCode, applyType);
        PlcItemEntity item = itemDao.selectOne(qw);
        if (item == null) {
            pCode = "signalJudge_error_001";
            pMsg = "PLC_ITEM表中未配置STN_NO为[" + stnNo + "]ITEM_CODE为[" + applyType + "]的点!";
            logWorkService.workLogging(workName, contNo, pCode, pMsg, "", workProc, "1002L", 0);
            return false;
        }
        if ("GET".equals(applyType) && "01".equals(item.getItemValue())) {
            logWorkService.workLogging(workName, contNo, pCode, pMsg, "", workProc, "1002L", 0);
            return true;
        }
        if ("PUT".equals(applyType) && "00".equals(item.getItemValue())) {
            logWorkService.workLogging(workName, contNo, pCode, pMsg, "", workProc, "1002L", 0);
            return true;
        }
        pCode = "signalJudge_error_002";
        pMsg = "CTU在站点[" + stnNo + "]进行[" + applyType + "]请求失败!料箱[" + contNo + "]线体暂不可取放!请等待...";
        logWorkService.workLogging(workName, contNo, pCode, pMsg, "", workProc, "1002L", 0);
        return false;
    }
}
